<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片轮播</title>
    <style>
        .carousel {
            width: 80%;
            margin: 20px auto;
            overflow: hidden;
            position: relative;
        }
        .carousel-inner {
            display: flex;
            transition: transform 0.5s ease-in-out;
        }
        .carousel-item {
            min-width: 100%;
            box-sizing: border-box;
        }
        .carousel-item img {
            width: 100%;
            display: block;
        }
        .carousel-controls {
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            display: flex;
            justify-content: space-between;
            width: 100%;
            padding: 0 20px;
            box-sizing: border-box;
        }
        .carousel-controls button {
            background: rgba(0, 0, 0, 0.5);
            color: white;
            border: none;
            padding: 10px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="carousel">
        <div class="carousel-inner">
            <div class="carousel-item">
                <img src="1.jpg" alt="Image 1">
            </div>
            <div class="carousel-item">
                <img src="2.jpg" alt="Image 2">
            </div>
            <div class="carousel-item">
                <img src="3.jpg" alt="Image 3">
            </div>
        </div>
        <div class="carousel-controls">
            <button id="prev">&#10094;</button>
            <button id="next">&#10095;</button>
        </div>
    </div>

    <script>
        const carouselInner = document.querySelector('.carousel-inner');
        const items = document.querySelectorAll('.carousel-item');
        const totalItems = items.length;
        let currentIndex = 0;

        // 添加自动切换功能
        let autoSlideInterval;

        function startAutoSlide(interval) {
            autoSlideInterval = setInterval(() => {
                currentIndex = (currentIndex + 1) % totalItems;
                updateCarousel();
            }, interval);
        }

        function stopAutoSlide() {
            clearInterval(autoSlideInterval);
        }

        document.getElementById('next').addEventListener('click', () => {
            currentIndex = (currentIndex + 1) % totalItems;
            updateCarousel();
            // 重新开始自动切换
            stopAutoSlide();
            startAutoSlide(3000);
        });

        document.getElementById('prev').addEventListener('click', () => {
            currentIndex = (currentIndex - 1 + totalItems) % totalItems;
            updateCarousel();
            // 重新开始自动切换
            stopAutoSlide();
            startAutoSlide(3000);
        });
        
        document.querySelectorAll('.tab-button').forEach(button => {
            button.addEventListener('click', () => {
                currentIndex = parseInt(button.getAttribute('data-index'));
                updateCarousel();
                // 更新选项卡的激活状态
                document.querySelectorAll('.tab-button').forEach(btn => btn.classList.remove('active'));
                button.classList.add('active');
            });
        });

        function updateCarousel() {
            const offset = -currentIndex * 100;
            carouselInner.style.transform = `translateX(${offset}%)`;
        }

        // 启动自动切换
        startAutoSlide(3000);
    </script>
    <!-- asd -->
</body>
</html>